﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SHW_UP.BP;

namespace SHW_UP.UI
{
    public partial class frmControl : Form
    {
        public frmControl()
        {
            InitializeComponent();
        }

        private void bindCombox()
        {
            DataTable dtmode = new DataTable();
            dtmode.Columns.Add("Value");
            dtmode.Columns.Add("Name");
            DataRow drSex;
            drSex = dtmode.NewRow();
            drSex[0] = "0";
            drSex[1] = "0";
            dtmode.Rows.Add(drSex);
            drSex = dtmode.NewRow();
            drSex[0] = "1";
            drSex[1] = "1";
            dtmode.Rows.Add(drSex);
            drSex = dtmode.NewRow();
            drSex[0] = "2";
            drSex[1] = "2";
            dtmode.Rows.Add(drSex);
            this.modeCombox.ValueMember = "Value";
            this.modeCombox.DisplayMember = "Name";
            this.modeCombox.DataSource = dtmode;
            this.modeCombox.DropDownStyle = ComboBoxStyle.DropDownList;

            DataTable dtsensor = new DataTable();
            dtsensor.Columns.Add("Value");
            dtsensor.Columns.Add("Name");
            DataRow drSexs;
            drSexs = dtsensor.NewRow();
            drSexs[0] = "1";
            drSexs[1] = "关闭";
            dtsensor.Rows.Add(drSexs);
            drSexs = dtsensor.NewRow();
            drSexs[0] = "0";
            drSexs[1] = "打开";
            dtsensor.Rows.Add(drSexs);
            this.sensorCombox.ValueMember = "Value";
            this.sensorCombox.DisplayMember = "Name";
            this.sensorCombox.DataSource = dtsensor;
            this.sensorCombox.DropDownStyle = ComboBoxStyle.DropDownList;
        }
        private DataTable dt = new DataTable();
        
        private void frmControl_Load(object sender, EventArgs e)
        {
            
            bindCombox();
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.ReadOnly = true;
            dt.Columns.Add("GPRS时间间隔");
            dt.Columns.Add("工作模式");
            dt.Columns.Add("GPS时间间隔");
            dt.Columns.Add("传感器");
            DataRow dr1 = dt.NewRow();
            dr1[0] = InfoUtils.parTable["gprstime"];
            dr1[1] = InfoUtils.parTable["mode"];
            dr1[2] = InfoUtils.parTable["gpstime"];
            dr1[3] = InfoUtils.parTable["关闭"];
            dt.Rows.Add(dr1);
            this.dataGridView1.DataSource = dt;
        }

        private void controlModify_Click(object sender, EventArgs e)
        {
            string passwd = string.Empty;
            if (InfoUtils.parTable.Contains("passwd"))
            {
                passwd = (string)InfoUtils.parTable["passwd"];
            }
            else
            {
                passwd = "000000";
            }
             int gprstime = Convert.ToInt32(this.gprsHourNum.Value) * 3600 + Convert.ToInt32(this.gprsMinNum.Value) * 60 + Convert.ToInt32(this.gprsSecNum.Value);
            if (gprstime < 10)
            {
                gprstime = 10;
            }
            string str = "GPRSDS," + passwd + "," + gprstime + ",";
            //InfoUtils.sendInfoTable.Add("GPRSDS", str);
            str = "XGMS," + passwd + "," + this.modeCombox.SelectedValue + ",";
            if (InfoUtils.sendInfoTable.ContainsKey("XGMS"))
            {
                InfoUtils.sendInfoTable.Remove("XGMS");
            }
            InfoUtils.sendInfoTable.Add("XGMS", str);
            int gpstime = Convert.ToInt32(this.gpsHourNum.Value) * 3600 + Convert.ToInt32(this.gpsMinNum.Value) * 60 + Convert.ToInt32(this.gpsSecNum.Value);
            if (gpstime < 60)
            {
                gpstime = 60;
            }
            str = "DSSJ," + passwd + "," + gpstime + ","; ;
            if (InfoUtils.sendInfoTable.ContainsKey("DSSJ")) 
            {
                InfoUtils.sendInfoTable.Remove("DSSJ");
            }
            InfoUtils.sendInfoTable.Add("DSSJ", str);
            this.dataGridView1.Rows[0].Cells[0].Value=gpstime;
            this.dataGridView1.Rows[0].Cells[1].Value=this.modeCombox.SelectedText;;
            this.dataGridView1.Rows[0].Cells[2].Value=gpstime;
            this.dataGridView1.Rows[0].Cells[3].Value=this.sensorCombox.SelectedText;
            //SerialPortSender.sendStrInfo(str, this.serialPort);
        }

        private void GridView_RowClick(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                int gprst = 0;
                int gpst = 0;
                try
                {
                    gprst = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                    gpst = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString());
                }
                catch (Exception se)
                {
                    Console.WriteLine(se.Message);
                }

                if (gprst == 60)
                {
                    this.gprsHourNum.Value = 0;
                    this.gprsMinNum.Value = 1;
                    this.gprsSecNum.Value = 0;
                }
                else if (gprst < 60)
                {
                    this.gprsHourNum.Value = 0;
                    this.gprsMinNum.Value = 0;
                    this.gprsSecNum.Value = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                }
                else
                {
                    this.gprsHourNum.Value = 0;
                    this.gprsMinNum.Value = gprst / 60;
                    this.gprsSecNum.Value = gprst % 60;
                }
                if (gpst == 60)
                {
                    this.gpsHourNum.Value = 0;
                    this.gpsMinNum.Value = 1;
                    this.gpsSecNum.Value = 0;
                }
                else if (gpst < 60)
                {
                    this.gpsHourNum.Value = 0;
                    this.gpsMinNum.Value = 0;
                    this.gpsSecNum.Value = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString());
                }
                else
                {
                    this.gpsHourNum.Value = 0;
                    this.gpsMinNum.Value = gpst / 60;
                    this.gpsSecNum.Value = gpst % 60;
                }
                this.modeCombox.SelectedText = this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                this.sensorCombox.SelectedText = this.dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void controlRefresh_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows[0].Cells[0].Value = InfoUtils.parTable["gprstime"];
            this.dataGridView1.Rows[0].Cells[1].Value = InfoUtils.parTable["mode"];
            this.dataGridView1.Rows[0].Cells[2].Value = InfoUtils.parTable["gpstime"];
            this.dataGridView1.Rows[0].Cells[3].Value = InfoUtils.parTable["关闭"];
            this.dataGridView1.ClearSelection();
        }
    }
}
